package com.suneast.suneaststore.modules.homepage.servicemall;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.msg.chat.ChatFragment;
import com.suneast.suneaststore.ui.fragments.BaseFragment;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;


public class MachiningFragment extends BaseFragment {
    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_machining;
    }

    @OnClick({R.id.img_back, R.id.btn_chat})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.img_back: {
                pop();
            }
            break;
            case R.id.btn_chat: {
                start(new ChatFragment());
            }
            break;
            case R.id.btn_buy_it_now: {

            }
            break;
        }
    }

    @BindView(R.id.img_machining)
    ImageView machiningImg;
    @BindView(R.id.sliding_tabs)
    TabLayout slidingTabs;
    @BindView(R.id.rv_calendar)
    RecyclerView calendarRv;
    CalendarAdapter calendarAdapter;
    ArrayList<CalendarBean> list;

    ImageView caseImg;

    LinearLayoutCompat footerView;

    //
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

        Glide.with(getContext()).load(API.SITE + "/attachment/images/global/service-banner4.jpg").into(machiningImg);
        list = createCalendarBean();
        slidingTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.e("test", "onTabSelected: pos = " + tab.getPosition());
                switch (tab.getPosition()) {
                    case 0: {
                        calendarRv.scrollToPosition(0);
                    }
                    break;
                    case 1: {
                        calendarRv.scrollToPosition(0);
                    }
                    break;
                    case 2: {
                        calendarRv.scrollToPosition(list.size() + 2 - 1);
                    }
                    break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        calendarRv.setLayoutManager(new LinearLayoutManager(getContext()));
        calendarAdapter = new CalendarAdapter(R.layout.item_calendar, list);
        calendarAdapter.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.layout_calendar_header, null));
        footerView = (LinearLayoutCompat) LayoutInflater.from(getContext()).inflate(R.layout.layout_calendar_footer, null);
        caseImg = footerView.findViewById(R.id.img_case);
        calendarAdapter.addFooterView(footerView);
        calendarRv.setAdapter(calendarAdapter);

        Glide.with(getContext()).load(API.SITE + "/attachment/images/global/clean/case.jpg").into(caseImg);
    }

    private ArrayList<CalendarBean> createCalendarBean() {
        ArrayList<CalendarBean> list = new ArrayList<>();

        LocalDate today = LocalDate.now();
        final int todayInMonth = today.getDayOfMonth();
        final int todayInWeek = today.getDayOfWeek();
        final int theRestOfDaysInMonth = getDaysOfMonth(today) - todayInMonth + 1;

        ArrayList<Integer> dayNumberList01 = new ArrayList<>();
        for (int i = 0; i < todayInWeek - 1; i++) {
            dayNumberList01.add(-1);
        }
        for (int i = 0; i < theRestOfDaysInMonth; i++) {
            dayNumberList01.add(todayInMonth + i);
        }
        CalendarBean curMonthBean = new CalendarBean(today.getYear(), today.getMonthOfYear(), dayNumberList01);
        list.add(curMonthBean);

        Calendar calendar = Calendar.getInstance();
        if (today.getMonthOfYear() == 12) {
            calendar.set(Calendar.YEAR, today.getYear() + 1);
            calendar.set(Calendar.MONTH, 0);
        } else {
            calendar.set(Calendar.YEAR, today.getYear());
            calendar.set(Calendar.MONTH, today.getMonthOfYear());
        }
        calendar.set(Calendar.DATE, 1);
        LocalDate nextMonthDay = LocalDate.fromCalendarFields(calendar);
        int nextMonthFisrtDayInWeek = nextMonthDay.getDayOfWeek();
        int nextMonthDays = getDaysOfMonth(nextMonthDay);

        ArrayList<Integer> dayNumberList02 = new ArrayList<>();
        for (int i = 0; i < nextMonthFisrtDayInWeek - 1; i++) {
            dayNumberList02.add(-1);
        }
        for (int i = 0; i < nextMonthDays; i++) {
            dayNumberList02.add(1 + i);
        }
        CalendarBean nextMonthBean = new CalendarBean(nextMonthDay.getYear(), nextMonthDay.getMonthOfYear(), dayNumberList02);
        list.add(nextMonthBean);

        return list;
    }


    private int getDaysOfMonth(LocalDate localDate) {
        DateTime dateTime = new DateTime(localDate.getYear(), localDate.getMonthOfYear(), 14, 12, 0, 0, 000);
        return dateTime.dayOfMonth().getMaximumValue();
    }
}
